package com.lovepick;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

import com.lovepick.bean.User;
import com.lovepick.http.HttpThread;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.Constant;


/**
 * @author MR
 */
public class LoginActivity extends Activity
{
	private static final String LOGIN_TAG = "LoginActivity";
	private static final String LOGIN_ID = "ID";
	private static final String LOGIN_PASSWORD = "Password";
	private static final String LOGIN_REMEMBER_ME = "rememberMe";

	private EditText edtNickName;
	private EditText edtPassword;

	private String mNickName;
	private String mPassWord;
	private ProgressDialog mDialog;
	private HttpThread mThread;
	private LoginHandler mHandler;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		Bundle data = this.getIntent().getBundleExtra("login");
		setContentView(R.layout.login);
		findView();
		mDialog = new ProgressDialog(this);
		mHandler = new LoginHandler(this);

		if (data != null)
		{
			Log.i(LOGIN_TAG, data.toString());
			edtNickName.setText(data.getString("username"));
			edtPassword.setText(data.getString("password"));
			doLogin(null);
		}
		else
		{
			User user = GlobalApp.getInstance().getLoginInfo();
			if (user != null)
			{
				Log.i(LOGIN_TAG, "自动登录");
				Intent intent = new Intent(LoginActivity.this, MainActivity.class);
				startActivity(intent);
				finish();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		return super.onCreateOptionsMenu(menu);
	}

	private void findView()
	{
		edtNickName = (EditText) this.findViewById(R.id.edt_nickname_login);
		edtPassword = (EditText) this.findViewById(R.id.edt_password_login);
	}

	/**
	 * 登录按钮事件
	 *
	 * @param v 包含登录按钮的视图
	 */
	public void doLogin(View v)
	{
		mNickName = edtNickName.getText().toString();
		Log.i(LOGIN_TAG, "NickName:" + mNickName);
		mPassWord = edtPassword.getText().toString();
		Log.i(LOGIN_TAG, "PassWord:" + mPassWord);

		if (BaseUtils.isEmptyString(mNickName) || BaseUtils.isEmptyString(mPassWord))
		{
			BaseUtils.showToast(this, R.string.login_promp);
		}
		else if (!BaseUtils.getNetworkState())
		{
			BaseUtils.showToast(this, R.string.network_error);
		}
		else
		{
			final List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair(LOGIN_ID, mNickName));
			params.add(new BasicNameValuePair(LOGIN_PASSWORD, mPassWord));
			params.add(new BasicNameValuePair(LOGIN_REMEMBER_ME, "true"));

			mThread = new HttpThread(mHandler, Constant.HTTP_METHOD_POST, Constant.HTTP_URL_LOGIN, params);
			mThread.start();
			mDialog.setTitle(R.string.dialog_title_login);
			mDialog.setMessage(getString(R.string.dialog_msg_login));
			mDialog.show();
		}

	}

	public void doReg(View v)
	{
		Intent intent = new Intent(v.getContext(), RegActivity.class);
		startActivity(intent);
		finish();
	}

	private static class LoginHandler extends Handler
	{
		private final String LOGIN_HANDLER_TAG = "LoginHandler";
		private WeakReference<LoginActivity> mReference;
		private LoginActivity mActivity;

		public LoginHandler(LoginActivity activity)
		{
			mReference = new WeakReference<LoginActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg)
		{
			mActivity = mReference.get();
			switch (msg.what)
			{
				case Constant.MSG_HTTP_ERROR:
					mActivity.mDialog.dismiss();
					BaseUtils.showToast(mActivity, R.string.network_connnect_fail);
					break;

				case Constant.MSG_HTTP_SUCCESS:
					Log.i(LOGIN_HANDLER_TAG, "###get http result###");
					String result = msg.getData().getString(Constant.HTTP_RESULT);
					Log.i(LOGIN_HANDLER_TAG, "http_result:" + result);/*
					if (result.equals(Constant.HTTP_LOGIN_ERROR2))
					{
						Log.i(LOGIN_HANDLER_TAG, "login_not_match");
						BaseUtils.showToast(mActivity, R.string.login_not_match);
						mActivity.mDialog.dismiss();
					}
					else if (!result.equals(Constant.HTTP_LOGIN_ERROR))
					{
						Log.i(LOGIN_HANDLER_TAG, "login_success");
						BaseUtils.showToast(mActivity, R.string.login_success);

						User user = new User();
						user.setNickName(mActivity.mNickName);
						user.setPassWord(mActivity.mPassWord);
						String[] tmp = result.split(";");
						user.setAvatar(tmp[0]);
						user.setUserID(tmp[1]);
						Log.i(LOGIN_HANDLER_TAG, "save login info");
						GlobalApp.getInstance().setLoginInfo(user);
						Intent intent = new Intent(mActivity, MainActivity.class);
						intent.putExtra("mNickName", mActivity.mNickName);
						mActivity.startActivity(intent);
						mActivity.mDialog.dismiss();
						mActivity.finish();

					}
					else
					{
						Log.i(LOGIN_HANDLER_TAG, "login_fail ");
						BaseUtils.showToast(mActivity, R.string.login_fail);
						mActivity.mDialog.dismiss();
					}
					*/
					if (result.startsWith(Constant.HTTP_LOGIN_SUCCESS))
					{
						// login_ok;avtar;userid
						Log.i(LOGIN_HANDLER_TAG, "login_success");
						BaseUtils.showToast(mActivity, R.string.login_success);

						User user = new User();
						user.setNickName(mActivity.mNickName);
						user.setPassWord(mActivity.mPassWord);
						String[] tmp = result.split(";");
						user.setAvatar(tmp[1]);
						user.setUserID(tmp[2]);
						Log.i(LOGIN_HANDLER_TAG, "save login info");
						GlobalApp.getInstance().setLoginInfo(user);
						Intent intent = new Intent(mActivity, MainActivity.class);
						intent.putExtra("mNickName", mActivity.mNickName);
						mActivity.startActivity(intent);
						mActivity.mDialog.dismiss();
						mActivity.finish();
					}
					else
					{
						Log.i(LOGIN_HANDLER_TAG, "login_fail ");
						BaseUtils.showToast(mActivity, R.string.login_fail);
						mActivity.mDialog.dismiss();
					}
					break;

				default:
					mActivity.mDialog.dismiss();
					break;
			}
		}
	}

}
